package com.itheima.tanhua.dubbo.mongo.api.impl;

import com.itheima.tanhua.api.TanHuaApi;
import com.itheima.tanhua.mongo.RecommendUser;
import com.itheima.tanhua.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.BsonInt64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

/**
 * @author Lff
 * @date 2022/3/25
 */

@DubboService
public class TanHuaApiImpl implements TanHuaApi {

    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public RecommendUser queryByToUserIdWithScoreMax(Long id) {
        //创建Criteria对象，构造条件
        Criteria c = Criteria.where("toUserId").is(new BsonInt64(id));
        Query q = Query.query(c)
                .with(Sort.by(Sort.Order.desc("score")))
                .limit(1);
        RecommendUser today = mongoTemplate.findOne(q, RecommendUser.class);
        return today;
    }

}
